package com.wish.g1.accounting.util;

import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.model.PayOrderQueryReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.request.PayOrderQueryRequest;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.jeequan.jeepay.response.PayOrderQueryResponse;
import com.wish.g1.accounting.config.JeepayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 阿里支付宝工具类
 */
@Slf4j
@Component
public class AlipayUtils {

    @Autowired
    JeepayConfig jeepayConfig;

    /**
     * 下单方法
     * @param orderNo 订单号
     * @param amount 订单金额
     * @param subject 订单标题
     * @param body 订单描述
     * @return
     */
    public PayOrderCreateResponse payOrder(String orderNo, long amount, String subject, String body){

        //创建客户端
        JeepayClient jeepayClient = JeepayClient.getInstance(jeepayConfig.getAppId(), jeepayConfig.getApiKey(),jeepayConfig.getApiBase());

        //构建请求数据
        PayOrderCreateRequest request = new PayOrderCreateRequest();
        PayOrderCreateReqModel model = new PayOrderCreateReqModel();
        // 商户号
        model.setMchNo(jeepayConfig.getMchNo());
        // 应用ID
        model.setAppId(jeepayClient.getAppId());
        // 商户订单号
        model.setMchOrderNo(orderNo);
        // 支付方式
        model.setWayCode(jeepayConfig.getWayCode());
        // 金额，单位分=================
        model.setAmount(amount);
        // 币种，目前只支持cny
        model.setCurrency(jeepayConfig.getCurrency());
        // 发起支付请求客户端的IP地址
        model.setClientIp(jeepayConfig.getClientIp());
        // 商品标题==============
        model.setSubject(subject);
        // 商品描述==============
        model.setBody(body);
        // 异步通知地址
        model.setNotifyUrl(jeepayConfig.getNotifyUrl());
        // 前端跳转地址
        model.setReturnUrl(jeepayConfig.getReturnUrl());

        request.setBizModel(model);
        // 发起统一下单
        PayOrderCreateResponse response = null;
        try {
            response = jeepayClient.execute(request);
            return response;

        } catch (JeepayException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 订单查询接口
     * @param mchOrderNo 商户订单号
     * @return
     */
    public PayOrderQueryResponse payOrderQuery(String mchOrderNo) {
        // 支付接口文档：https://docs.jeequan.com/docs/jeepay/payment_api
        JeepayClient jeepayClient = JeepayClient.getInstance(jeepayConfig.getAppId(), jeepayConfig.getApiKey(),jeepayConfig.getApiBase());
        PayOrderQueryRequest request = new PayOrderQueryRequest();
        PayOrderQueryReqModel model = new PayOrderQueryReqModel();
        // 商户号
        model.setMchNo(jeepayConfig.getMchNo());
        // 应用ID
        model.setAppId(jeepayConfig.getAppId());
        // 支付订单号
        model.setMchOrderNo(mchOrderNo);

        request.setBizModel(model);

        try {
            PayOrderQueryResponse response = jeepayClient.execute(request);
            log.info("验签结果：{}", response.checkSign(jeepayConfig.getApiKey()));

            if(response.isSuccess(jeepayConfig.getApiKey())) {
                log.info("订单信息：{}", response);
                log.info("金额：{}", response.get().getAmount());
                return response;
            }
        } catch (JeepayException e) {

            e.printStackTrace();
        }

        return null;
    }
}
